<template>
	<view class="content">
		<scroll-view scroll-y="true">
			<view class="uni-list-cell" hover-class="uni-list-cell-hover" @click="showIncreasePicker()">
				<view class="uni-list-cell-navigate uni-navigate-right">
					<text>增/减项</text>
					<text class="right-text">{{type_name}}</text>
				</view>
			</view>
			<mpvue-picker ref="mpvueIncreaseRef" :deepLength="1" @onConfirm="onIncreaseConfirm" :pickerValueArray="increaseArray"></mpvue-picker>

			<view class="uni-list-cell" hover-class="uni-list-cell-hover" @click="showTaskPicker()">
				<view class="uni-list-cell-navigate uni-navigate-right">
					<text>相关任务</text>
					<text class="right-text">{{project_name}}</text>
				</view>
			</view>
			<mpvue-picker ref="mpvueTaskRef" :deepLength="1" :pickerValueDefault="formData.task_id" @onConfirm="onTaskConfirm"
			 :pickerValueArray="taskArray"></mpvue-picker>

			<view class="uni-list-cell" hover-class="uni-list-cell-hover">
				<view class="uni-list-cell-navigate" :class="{'com-oppor-item': isDetail}">
					<text>金额</text>
					<input class="right-text" type="digit" v-model="formData.amount" />
				</view>
			</view>
			<view class="uni-list-cell-navigate">
				<text>说明：</text>
			</view>
			<view class="uni-list-cell" hover-class="uni-list-cell-hover">
				<view class="uni-list-cell-navigate">
					<textarea placeholder="请描述增/减项事由" v-model="formData.note"></textarea>
				</view>
			</view>
			<view>
				<cus-upload-image removeMark :imageList="formData.picture_data" imgTitle="上传图片" :imgSize="200"></cus-upload-image>
			</view>
			<button class="btn-primary" type="primary" @click="submit">保存</button>
		</scroll-view>
	</view>
</template>

<script>
	import {
		increaseArray
	} from '../../../common/stateData.js'
	export default {
		data() {
			return {
				isAdd: true, //是编辑还是新增（true是新增，false是编辑）
				increaseArray: increaseArray, //增减项列表
				taskArray: [], //相关任务列表
				order_id: '', //编辑页面需要的表id
				type_name: '', //增或减项目name
				project_name: '', //相关任务名称
				//页面数据
				formData: {
					project_id: null, //项目id
					picture_data: [], //图片
					note: '', //说明
					amount: null, //金额
					type: '', //增或减项目id
					task_id: '', //相关任务id
					user_id: '', //经办人
				}
			}
		},
		onLoad(param) {
			if (param.order_id) {
				//是编辑页面
				this.isAdd = false
				this.order_id = parseInt(param.order_id)
				this.getIncreaseDetail(parseInt(param.order_id))
			} else {
				//是新增页面
				this.isAdd = true
				uni.setNavigationBarTitle({
					title: "增项新增"
				})
			}
			this.formData.project_id = parseInt(param.hard_id) //任务id
			this.getSubTask(parseInt(param.hard_id)) //获取相关任务
		},
		methods: {
			//编辑页面获取增项详情
			getIncreaseDetail(order_id) {
				uni.showLoading({
					title: '',
					mask: false
				});
				uni.request({
					url: this.$store.state.baseUrl + '/lwj/app_add_sub_info',
					method: 'POST',
					header: {
						token: this.$store.state.token
					},
					data: {
						order_id
					},
					success: res => {
						if (res.data.code === 1) {
							uni.hideLoading()
							this.formData = {
								...this.formData,
								...res.data.data.info
							}
							this.type_name = this.formData.type !== '' ? this.formData.type === 'add' ? '增项' : '减项' : '',
								this.project_name = res.data.data.info.task_name
							res.data.data.info.att_list.map((item, index) => {
								this.formData.picture_data.push(item.imgurl)
							})
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			//获取相关任务信息
			getSubTask(hard_id) {
				uni.showLoading({
					title: '',
					mask: false
				});
				uni.request({
					url: this.$store.state.baseUrl + '/lwj/app_add_sub_task',
					method: 'POST',
					header: {
						token: this.$store.state.token
					},
					data: {
						hard_id
					},
					success: res => {
						uni.hideLoading()
						if (res.data.code === 1) {
							res.data.data.list.map(item => {
								this.taskArray.push({
									value: item.id,
									label: item.search_file
								})
							})
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			//点击增项选择器
			showIncreasePicker() {
				this.$refs.mpvueIncreaseRef.show()
			},
			//增项选择器确认
			onIncreaseConfirm(e) {
				this.formData.type = e.value[0]
				this.type_name = e.label
			},
			//点击相关任务选择器
			showTaskPicker() {
				if (this.taskArray.length === 0) {
					uni.showToast({
						title: '工序已全部完成或暂无相关任务分项',
						mask: false,
						duration: 1500,
						icon: 'none'
					});
				} else {
					this.$refs.mpvueTaskRef.show()
				}
			},
			//相关任务选择器确认
			onTaskConfirm(e) {
				this.formData.task_id = e.value[0]
				this.project_name = e.label
			},
			//点击完成提交信息
			submit() {
				this.formData.user_id = this.$store.state.uid
				this.formData.amount = parseFloat(this.formData.amount)
				if (this.isAdd) {
					//是新增页面
					this.addSubmit()
				} else {
					//是编辑页面
					this.editSubmit()
				}
			},
			//新增提交
			addSubmit() {
				uni.showLoading({
					title: '信息提交...',
					mask: false
				});
				uni.request({
					url: this.$store.state.baseUrl + '/lwj/app_add_sub_create',
					method: 'POST',
					data: {
						...this.formData
					},
					success: res => {
						uni.hideLoading()
						if (res.data.code === 1) {
							uni.showToast({
								title: '成功,返回列表',
								mask: false,
								duration: 1500
							});
							this.goIncreaseList() //跳回列表页
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			//编辑提交
			editSubmit() {
				uni.showLoading({
					title: '信息提交...',
					mask: false
				});
				uni.request({
					url: this.$store.state.baseUrl + '/lwj/app_add_sub_write',
					method: 'POST',
					data: {
						...this.formData,
						order_id: this.order_id
					},
					success: res => {
						uni.hideLoading()
						if (res.data.code === 1) {
							uni.showToast({
								title: '成功,返回列表',
								mask: false,
								duration: 1500
							});
							this.goIncreaseList() //跳回列表页
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			//跳回列表页面
			goIncreaseList() {
				this.$eventBus.$emit('increaseEditExecute')
				uni.navigateBack({
					delta: 1
				})
				// setTimeout(() => {
				// 	uni.navigateTo({
				// 		url: './increaseItemList',
				// 		success: res => {},
				// 		fail: () => {},
				// 		complete: () => {}
				// 	})
				// }, 1500)
			}
		},
	}
</script>

<style scoped>
	.right-text {
		text-align: right;
		margin-right: 25upx;
	}

	.content {
		margin-top: 10upx;
		background-color: #FFFFFF;
	}

	.uni-uploader-title {
		font-size: 30upx;
	}

	input {
		text-align: right;
	}

	.uni-list-cell-navigate text,
	.uni-list-cell-navigate input {
		flex: 1;
	}

	textarea {
		height: 150upx;
	}
</style>
